#include "wintime.h"



#define EPOCH_BASE  116444736000000000i64 // Number of 100 nanosecond units from 1/1/1601 to 1/1/1970

gee::uint64 gee::getWinTime()
{
	// Gets time in 100 nanosecond units
	FILETIME ft;
	GetSystemTimeAsFileTime(&ft);
	ULARGE_INTEGER ret;
	ret.HighPart = ft.dwHighDateTime;
	ret.LowPart = ft.dwLowDateTime;

	// Compensates epoch difference
	return ret.QuadPart - EPOCH_BASE;
}

time_t gee::SystemTimeToDosTime( SYSTEMTIME* st )
{
	uint64 utcDosTime = getWinTime()/10000000;

	return (time_t)utcDosTime;
}

void gee::DosTimeToSystem( time_t t,SYSTEMTIME* st )
{
	LARGE_INTEGER jan1970FT = {0};
	jan1970FT.QuadPart = EPOCH_BASE; // january 1st 1970

	LARGE_INTEGER utcFT = {0};
	utcFT.QuadPart = ((uint64)t)*10000000 + jan1970FT.QuadPart;

	if(st)
		FileTimeToSystemTime((FILETIME*)&utcFT, st);
}

gee::uint64 gee::getCurrentTimeMs()
{
	uint64 t = getWinTime();
	return t/10000i64;
}

gee::uint64 gee::getCurrentTimeUs()
{
	uint64 t = getWinTime();
	return t/10i64;
}
